package com.team.game.Screens;

import android.graphics.Canvas;
import android.view.MotionEvent;

import com.team.game.Game.Gestures;
import com.team.game.Game.MapHelper.Location;
import com.team.game.Game.MapHelper.Map;
import com.team.game.framework.Game;
import com.team.game.framework.Level;
import com.team.game.framework.Screen;
import com.team.game.framework.impl.SurfaceThread;

public class Maps_Screen extends Screen {

    private boolean LOCATION = false;

    private Map map;
    private Location location;

    Maps_Screen (Game game) {

        super(game);
        SurfaceThread.wait = true;

        map  = new Map (game);
        location = new Location (game);

        SurfaceThread.wait = false;
    }

    private boolean animate = true;
    private boolean locationORmap = false;

    @Override
    public void update () {
        if (LOCATION) {
            if( animate == false) {
                if(locationORmap == false) {
                    locationORmap = map.animate();
                    if (locationORmap ==true)
                    {
                        location.border.start();
                        location.test ();
                    }
                }
                else {
                    animate = location.animate();
                    if(animate == true)
                    {
                        stopTouch = false;
                    }
                }
            }


            else {
                location.update();
            }
        }


        else {

            if( animate == false) {
                if(locationORmap == false) {
                    locationORmap = location.unroll();
                    if (locationORmap ==true)
                    {
                        map.border.start();
                    }
                }
                else {
                    animate = map.unroll();
                    if(animate == true)
                    {
                        stopTouch = false;
                    }
                }
            }

            else {
                map.update();
            }
        }

    }

    @Override
    public void present(Canvas canvas) {
        map.present (canvas);
        location.present (canvas);
    }

    @Override
    public void pause () {
    }

    @Override
    public void resume () {
    }

    @Override
    public void dispose() {
    }

    @Override
    public void back() {
        if (LOCATION) {
            if (!stopTouch) {
                locationORmap = false;
                LOCATION = false;
                stopTouch = true;
                animate = false;
                location.border.disappear();
            }
        }
        else
            game.setScreen (new Main_Screen (game));
    }

    @Override
    public Level getLevel() {
        return null;
    }

    @Override
    public Gestures getGestures() {
        return null;
    }

    private boolean isBorderBool = false;
    private boolean stopTouch = false;

    @Override
    public boolean touchEvent (MotionEvent motionEvent) {

        if(!stopTouch) {
            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if (LOCATION) {
                        location.OnScroll(motionEvent);
                    } else {
                        map.OnScroll(motionEvent); //обрабатываем скролл карты
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (LOCATION) {

                    } else {
                        map.OnScroll(motionEvent); //обрабатываем скролл карты
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if (LOCATION) {
                        if (location.OnScroll(motionEvent)) {
                            locationORmap = false;
                            LOCATION = false;
                            stopTouch = true;
                            animate = false;
                            location.border.disappear();
                        }
                    } else {
                        int loc = map.OnScroll(motionEvent); //обрабатываем скролл карты
                        if (loc != 0) {
                            locationORmap = false;
                            LOCATION = true;
                            stopTouch = true;
                            animate = false;
                            map.border.disappear();
                        }
                    }
                    break;
            }
        }
        return true;
    }



}

//   private void setScroll(float rX, float rY, float bX, float bY){
//       //если после скрола выйдем за рамки битмапа - не скролить
//
//       if(rX + bX > 0 || rX + bX < maxX)
//           scrollX = 0; //чтобы не выйти за рамки карты
//       else
//           scrollX =  bX;
//
//       if(rY + bY > 0 || rY + bY < maxY)
//           scrollY = 0;
//       else
//           scrollY =  bY;
//   }

//        Log.d("log",  ""+fullmap.getByteCount() / 1024);
//        Log.i("logik", String.format("Total memory = %s",
//                (int) (Runtime.getRuntime().totalMemory() / 1024)));
//        final Runtime runtime = Runtime.getRuntime();
//        final long usedMemInMB=(runtime.totalMemory() - runtime.freeMemory()) / 1048576L;
//        final long maxHeapSizeInMB=runtime.maxMemory() / 1048576L;
//        final long availHeapSizeInMB = maxHeapSizeInMB - usedMemInMB;
//        Log.i("logikov","" + usedMemInMB+ ", " +maxHeapSizeInMB+", " +availHeapSizeInMB);